package com.dianban.controller.nengliang;

import com.dianban.base.ResultCode;
import com.dianban.base.ResultVo;
import com.dianban.config.redis.RedisConstant;
import com.dianban.config.redis.RedisUtil;
import com.dianban.controller.BaseController;
import com.dianban.entity.FacePayConfig;
import com.dianban.entity.StoreGuide;
import com.dianban.entity.User;
import com.dianban.service.FacePayConfigService;
import com.dianban.service.StoreGuideService;
import com.dianban.service.UserService;
import com.dianban.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "ApiH5UserController", description = "用户控制器")
@RestController
@RequestMapping(value = "${interfacePath}/nengliang/h5user/")
public class ApiH5UserController extends BaseController {

    @Value("${rongyaotuadmin.domain}")
    private String rongyaotuadminDomain;

    @Autowired
    private StoreGuideService storeGuideService;
    @Autowired
    private FacePayConfigService facePayConfigService;
    @Autowired
    private UserService userService;

    @ApiOperation(notes = "logout", httpMethod = "GET", value = "用户登出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token", required = true, paramType = "header", dataType = "string"),
    })
    @RequestMapping(value = "logout", method = RequestMethod.GET)
    public ResultVo logout(HttpServletRequest request, HttpServletResponse response) {
        UserUtils.logoutUser(request);
        return ResultVo.success("", "操作成功");
    }

    @ApiOperation(notes = "info", httpMethod = "GET", value = "获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token", required = true, paramType = "header", dataType = "string"),
    })
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public ResultVo info(HttpServletRequest request, HttpServletResponse response) {
        String userToken = request.getHeader(UserUtils.USER_TOKEN);
        String key = RedisConstant.USER_TOKEN_KEY + userToken;
        User user = (User) RedisUtil.get(key);
        if (user == null) {
            return ResultVo.error(ResultCode.ERROR, "请求头userToken过期");
        }
        StoreGuide storeGuide = storeGuideService.findByUserId(user.getId());
        if (storeGuide != null) {
            user.setIsStoreGuide(1);
            user.setStoreGuideId(storeGuide.getId());
        }
        List<FacePayConfig> facePayConfigList = facePayConfigService.find(90, 1, null);
        String showAccountAuth = "0";
        if (facePayConfigList != null && !facePayConfigList.isEmpty()) {
            showAccountAuth = facePayConfigList.get(0).getValue();
        }
        user.setShowAccountAuth(showAccountAuth);

        List<FacePayConfig> facePayConfigList2 = facePayConfigService.find(100, 1, null);
        String showAccountAuthSlj = "0";
        if (facePayConfigList2 != null && !facePayConfigList2.isEmpty()) {
            showAccountAuthSlj = facePayConfigList2.get(0).getValue();
        }
        user.setShowAccountAuthSlj(showAccountAuthSlj);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("user", user);
        return ResultVo.success(resultMap, "获取成功");
    }

    @ApiOperation(notes = "app", httpMethod = "GET", value = "获取app相关信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token", required = true, paramType = "header", dataType = "string"),
    })
    @RequestMapping(value = "app", method = RequestMethod.GET)
    public ResultVo app(HttpServletRequest request, HttpServletResponse response) {
        String userToken = request.getHeader(UserUtils.USER_TOKEN);
        String key = RedisConstant.USER_TOKEN_KEY + userToken;
        User user = (User) RedisUtil.get(key);
        if (user == null) {
            return ResultVo.error(ResultCode.ERROR, "请求头userToken过期");
        }
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("invitationUrl", rongyaotuadminDomain + "/register/#/?invitation=" + user.getInvitecode());
            resultMap.put("appDownloadUrl", rongyaotuadminDomain + "/index/mobile/download");
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultVo.error(ResultCode.ERROR, "获取失败");
        }
        return ResultVo.success(resultMap, "获取成功");
    }

    @ApiOperation(notes = "firstChildren", httpMethod = "GET", value = "获取我的推荐下级人员相关信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token", required = true, paramType = "header", dataType = "string"),
    })
    @RequestMapping(value = "firstChildren", method = RequestMethod.GET)
    public ResultVo firstChildren(HttpServletRequest request, HttpServletResponse response) {
        String userToken = request.getHeader(UserUtils.USER_TOKEN);
        String key = RedisConstant.USER_TOKEN_KEY + userToken;
        User user = (User) RedisUtil.get(key);
        if (user == null) {
            return ResultVo.error(ResultCode.ERROR, "请求头userToken过期");
        }
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<User> children = userService.findFirstChildren(user.getId());
            resultMap.put("list", children);
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultVo.error(ResultCode.ERROR, "获取失败");
        }
        return ResultVo.success(resultMap, "获取成功");
    }

}
